from moodlefuse.parsers import Parser
from files import Forum, Discussion
from moodlefuse.settings import MOODLE_WEBSITE_URL

class ForumsParser(Parser):

    def parse_forums(self):
        soup = self.get_parsed_data()
        forums_html = soup.findAll('td', attrs={'class': 'cell c0'})

        return self.parse_html(forums_html, Forum)

    def build_url(self):
        # .../course/view.php?id=111 -> .../mod/forum/index.php?id=111
        return self.url.replace('course/', 'mod/forum/').replace('view.php', 'index.php')

    def parse_item_name(self, html):
        return html.find('a').text.encode('utf-8')

    def parse_item_url(self, html):
        # returns only view.php...
        url = html.find('a')['href']
        return '%s/mod/forum/%s' % (MOODLE_WEBSITE_URL, url)

class DiscussionParser(Parser):

    def parse_discussion(self):
        soup = self.get_parsed_data()
        discussions_html = soup.findAll('table', attrs={'class': 'forumpost'})

        content = ""
        for discussion_html in discussions_html:
            content += "%s \n %s \n\n" % (discussion_html.find('div', attrs={'class': 'author'}).getText().encode('utf-8'),
                                          discussion_html.find('div', attrs={'class': 'posting'}).getText().encode('utf-8'))

            print content


        return content

class DiscussionsParser(Parser):

    def parse_discussions(self):
        soup = self.get_parsed_data()
        discussions_html = soup.findAll('td', attrs={'class': 'topic starter'})
        
        return self.parse_html(discussions_html, Discussion)

    def parse_item_name(self, html):
        return html.find('a').text.encode('utf-8')

    def parse_item_url(self, html):
        return html.find('a')['href']